//
//  HMReachable.m
//  YaoYDCoach
//
//  Created by holin on 6/2/15.
//  Copyright (c) 2015 1YD.ME. All rights reserved.
//

#import "HMReachable.h"
#import "Reachability.h"

@interface HMReachable()

@property (nonatomic) Reachability *internetReachability;

@end

@implementation HMReachable


- (BOOL)reachable {
  
  if (!_internetReachability) {
    return NO;
  }
  
  NetworkStatus netStatus = [_internetReachability currentReachabilityStatus];
  if (netStatus != NotReachable) {
    return YES;
  }
  
  return NO;
}

- (void)reachabilityChanged:(NSNotification *)note
{
  Reachability* curReach = [note object];
  NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
  [self updateInterfaceWithReachability:curReach];
}

- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
  NetworkStatus netStatus = [reachability currentReachabilityStatus];
  if (netStatus == NotReachable) {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"networkError" object:nil];
  }

}



+ (instancetype)sharedManager {
  static HMReachable *sharedMyManager = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedMyManager = [[self alloc] init];
  });
  return sharedMyManager;
}

- (id)init {
  if (self = [super init]) {
    self.internetReachability = [Reachability reachabilityForInternetConnection];
    [self.internetReachability startNotifier];
    [self updateInterfaceWithReachability:self.internetReachability];

  }
  return self;
}


@end
